package com.example.common.widget

import android.annotation.SuppressLint
import android.app.Activity
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.annotation.ColorInt
import com.example.common.R
import com.example.common.listener.PopMenuBean
import com.example.common.tools.LogUtils
import com.example.common.tools.dip2px
import com.example.common.tools.getWinHeight


@SuppressLint("AppCompatCustomView")
class PopMenuButton : AdapterView.OnItemClickListener {

    private var mCtx: Activity? = null
    private var popupWindow: PopupWindow? = null

    private var listView: ListView? = null

    var listener: OnItemListener? = null

    var mData: ArrayList<PopMenuBean> = ArrayList()

    private var itemBgColor: Int? = null
    private var textColor: Int? = null

    private constructor(context: Activity, list: ArrayList<PopMenuBean>) {
        mCtx = context
        mData = list
    }

    companion object {
        fun create(context: Activity, list: ArrayList<PopMenuBean>): PopMenuButton {
            return PopMenuButton(context, list)
        }

        interface OnItemListener {
            fun onItem(bean: PopMenuBean)
        }
    }


    private fun initListView() {
        listView = ListView(mCtx)
        listView!!.dividerHeight = 0
//        listView!!.setBackgroundResource(R.drawable.listview_background)
        listView!!.onItemClickListener = this
        var adapter: MyAdapter
        if (itemBgColor != null && textColor != null) {
            adapter = MyAdapter(itemBgColor!!, textColor!!)
        } else {
            adapter = MyAdapter()
        }
        listView!!.adapter = adapter
    }


    fun showPopupWindow(view: View): PopMenuButton {
        initListView()
        val lp = mCtx!!.window.attributes
        lp.alpha = 0.7f //设置阴影透明度
        mCtx!!.window.attributes = lp
        var itemY = view.context.dip2px(40f)
        var max = itemY * 7
        var height = itemY * mData.size
        if (height > max) {
            height = max
        }
        popupWindow = MyPopupWindow(listView!!, view.width, height.toInt())
        // 设置点击外部区域, 自动隐藏
        popupWindow!!.isOutsideTouchable = true // 外部可触摸
        // 设置空的背景, 响应点击事件
//        popupWindow!!.setBackgroundDrawable(ColorDrawable(0xb0000000.toInt()))
        popupWindow!!.isFocusable = true //设置可获取焦点
        var winHeight = mCtx!!.getWinHeight()
        // 显示在指定控件下
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        val x = location[0]
        val y = location[1]
        var remainHeight = y + popupWindow!!.height + mCtx!!.dip2px(35f)
        LogUtils.eLog(
            "Popup",
            "winHeight = $winHeight   y = $y   popupWindowheight = ${popupWindow!!.height}"
        )
        if (remainHeight > winHeight) {
            popupWindow!!.showAtLocation(view, Gravity.NO_GRAVITY, x, y - popupWindow!!.height)
        } else {
            popupWindow!!.showAtLocation(view, Gravity.NO_GRAVITY, x, y + view.height)
        }
        return this
    }

    fun setItemBG(@ColorInt color: Int): PopMenuButton {
        this.itemBgColor = color
        return this
    }

    fun setTextColor(@ColorInt color: Int): PopMenuButton {
        this.textColor = color
        return this
    }

    inner class MyPopupWindow : PopupWindow {
        constructor(contentView: View, width: Int, height: Int) : super(contentView, width, height)

        override fun dismiss() {
            val lp = mCtx!!.window.attributes
            lp.alpha = 1f //设置阴影透明度
            mCtx!!.window.attributes = lp
            super.dismiss()
        }
    }

    inner class MyAdapter : BaseAdapter {

        var view: View? = null
        var tv_number: TextView? = null
        private var itemBgColor: Int? = null
        private var textColor: Int? = null

        constructor(@ColorInt itemBgColor: Int, @ColorInt textColor: Int) : super() {
            this.itemBgColor = itemBgColor
            this.textColor = textColor
        }

        constructor() : super()

        override fun getView(position: Int, convertView: View?, p2: ViewGroup?): View {

            if (convertView == null) {
                view = View.inflate(mCtx, R.layout.popmenu_item, null)
            } else {
                view = convertView
            }
            tv_number = view!!.findViewById(R.id.tv_item)
            tv_number!!.text = mData[position].name
            if (itemBgColor != null) {
                view!!.setBackgroundColor(itemBgColor!!)
            }
            if (textColor != null) {
                tv_number!!.setTextColor(textColor!!)
                view!!.findViewById<View>(R.id.item_line1).setBackgroundColor(textColor!!)
            }
            return view!!

        }

        override fun getItem(p0: Int): Any {
            return mData[p0]
        }

        override fun getItemId(p0: Int): Long {
            return p0.toLong()
        }

        override fun getCount(): Int {
            return mData.size
        }


    }

    override fun onItemClick(p0: AdapterView<*>?, p1: View?, position: Int, id: Long) {
        listener?.onItem(mData[position])
        popupWindow!!.dismiss() // 消失了
    }


}

